import Reflux from "reflux"
import GUtils from "@/utils";
import BaseAction, {addDefaultListerns} from "@/base/BaseAction";

const prefix = "/device/geekDevice/";
const api = {
    search: prefix + "search",
    sync: prefix + "sync",
    syncByGeekId: prefix + "syncByGeekId",
    testMqtt: prefix + "testMqtt",
    bind: prefix + "bind",
    reset: prefix + "reset",
    getShopDeviceList: prefix + "getShopDeviceList",
};

const createAction = () => {
    const Action = Reflux.createActions({
        ...BaseAction,
        sync: {asyncResult: true},
        syncByGeekId: {asyncResult: true},
        testMqtt: {asyncResult: true},
        preBind:{asyncResult: true},
        bind: {asyncResult: true},
        reset: {asyncResult: true},
        getShopDeviceList: {asyncResult: true}
    });

    addDefaultListerns(Action, api);

    Action.sync.listen(function () {
        GUtils.Ajax.post(api.sync, {}).then(this.completed, this.failed);
    });
    Action.syncByGeekId.listen(function (geekId) {
        GUtils.Ajax.post(api.syncByGeekId, {geekId}).then(this.completed, this.failed);
    });
    Action.testMqtt.listen(function (geekId) {
        GUtils.Ajax.post(api.testMqtt, {geekId}).then(this.completed, this.failed);
    });
    Action.bind.listen(function (geekId, deviceId, callback) {
        GUtils.Ajax.post(api.bind, {geekId, deviceId}).then(this.completed.bind(this,callback), this.failed);
    });
    Action.reset.listen(function (geekId) {
        GUtils.Ajax.post(api.reset, {geekId}).then(this.completed, this.failed);
    });
    Action.getShopDeviceList.listen(function (shopId, callback) {
        GUtils.Ajax.post(api.getShopDeviceList, {shopId}).then(this.completed.bind(this,callback), this.failed);
    });
    return Action;
};

export {api, createAction}